package cn.lanqiao.book.service.impl;

import cn.lanqiao.book.dao.IBookShelfDao;
import cn.lanqiao.book.dao.impl.BookShelfDaoImpl;
import cn.lanqiao.book.pojo.ShelfBook;
import cn.lanqiao.book.service.IBookShelfService;

import java.util.List;

/*
* 有关书架的服务
* */
public class BookShelfServiceImpl implements IBookShelfService {
    //将该Service工具类设计为单例模式
    private static BookShelfServiceImpl bookShelfService=null;
    private BookShelfServiceImpl(){}
    public static IBookShelfService getInstance(){
        if(bookShelfService==null){
            bookShelfService=new BookShelfServiceImpl();
            return bookShelfService;
        }else {
            return bookShelfService;
        }
    }
    private IBookShelfDao IBookShelfDao =new BookShelfDaoImpl();
    @Override
    public Boolean addBookToShelf(int book_id, String username) {

        //通过用户名获得用户id
        int user_id= IBookShelfDao.getUserId(username);
        //通过用户id获得书架id
        int shelf_id= IBookShelfDao.getShelfId(user_id,2);
        if(IBookShelfDao.add(shelf_id,book_id)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean deleteMyBookShelf(int book_id, String username) {
        //通过用户名获得用户id
        int user_id= IBookShelfDao.getUserId(username);
        //通过用户id获得书架id
        int shelf_id= IBookShelfDao.getShelfId(user_id,2);
        if(IBookShelfDao.delete(shelf_id,book_id)){
            return true;
        }else {
            return false;
        }
    }
    @Override
    public Boolean deleteTempBookShelf(int book_id, String username) {
        //通过用户名获得用户id
        int user_id= IBookShelfDao.getUserId(username);
        //通过用户id获得书架id
        int shelf_id= IBookShelfDao.getShelfId(user_id,1);
        if(IBookShelfDao.delete(shelf_id,book_id)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean addBookToShelf(int book_id,int chapter_id, String username) {
        //通过用户名获得用户id
        int user_id= IBookShelfDao.getUserId(username);
        //通过用户id获得书架id
        int shelf_id= IBookShelfDao.getShelfId(user_id,1);

        if(IBookShelfDao.add(shelf_id,book_id,chapter_id)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<ShelfBook> getBookShelf(String username) {
        int userId= IBookShelfDao.getUserId(username);
        int shelfId= IBookShelfDao.getShelfId(userId,2);
        List<ShelfBook> list= IBookShelfDao.getBookList(shelfId);
        return list;
    }
    @Override
    public List<ShelfBook> getTempBookShelf(String username) {
        int userId= IBookShelfDao.getUserId(username);
        int shelfId= IBookShelfDao.getShelfId(userId,1);
        List<ShelfBook> list= IBookShelfDao.getBookList(shelfId);
        return list;
    }
}
